<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,initial-scale=1.0,width=device-width" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>app_index</title>
    <link rel="stylesheet" type="text/css" href="./css/api.css" />
    <style type="text/css">
        html,
        body {
            height: 100%;
            overflow: hidden;
        }

        .div_index_newfi_ba {
            width: 100%;
            text-align: center;
        }

        .div_index_newfi_1 img {
            width: 95px;
            height: 95px;
        }

        .div_index_newfi_2 img {
            width: 140px;
            height: 23px;
            margin-top: 16px;
        }

        .div_index_agent_1 {
            width: 100%;
            height: 120px;
            background-repeat: no-repeat;
            background-size: 135px 120px;
            background-position: center;
        }

        .div_index_newfi_3 img {
            width: 102px;
            height: 16px;
            margin-top: 12px;
        }

        .div_index_title {
            position: absolute;
            bottom: 30px;
            font-size: 16px;
            color: #686868;
        }

        .div_animation_prompt {
            position: absolute;
            bottom: 100px;
        }

        .div_animation_prompt img {
            width: 40px;
            height: 40px;
        }
    </style>
</head>

<body>

    <div class="div_index_newfiba" style="display:none">
        <div class="div_index_newfi_1 div_index_newfi_ba"><img src="./image/welcome_logo.png"></img>
        </div>
        <div class="div_index_newfi_2 div_index_newfi_ba"><img src="./image/welcome_wifi.png"></img>
        </div>
        <div class="div_index_newfi_3 div_index_newfi_ba"><img src="./image/welcome_newfi.png"></img>
        </div>
    </div>
    <div class="div_index_agentba" style="display:none">
        <div class="div_index_agent_1 div_index_newfi_ba">
        </div>
    </div>
    <div class="div_index_newfi_ba div_animation_prompt">
        <img src="./image/loading_more.gif">
    </div>

    <div class="div_index_title div_index_newfi_ba">
        我行我速·飞一般的感触
    </div>

</body>
<script type="text/javascript" src="./script/api.js"></script>
<script type="text/javascript" src="./script/jquery.min.js"></script>
<script type="text/javascript" src="./script/md5.js"></script>
<script trpe="text/javascript" src="./script/common.js"></script>
<script trpe="text/javascript" src="./script/storageKey.js"></script>
<script trpe="text/javascript" src="./script/seekAgent.js"></script>
<script trpe="text/javascript" src="./script/jsUtil/login.js"></script>
<script type="text/javascript">
    apiready = function() {
        getInitDatas();
      //getInitDatas1();
    };

    //获取app 传过来的数据
    function getInitDatas() {

        api.addEventListener({
            name: 'appApicloudData'
        }, function(ret, err){
            console.log('.......收到原生的信息..........' + JSON.stringify(ret));
            if( ret ){
              var appData = ret.value;
              if(api.systemType === 'ios'){
                appData = JSON.parse(appData);
              }
              var wifiName = appData.wifiName; //wifi的名字
              var wifiIp = appData.wifiIp; //wifi的IP
              var apMac = appData.apMac; //APmac
              var phoneMac = appData.phoneMac; //设备mac
              $api.setStorage(NewFiData.wifiKey.wifiName, wifiName);
              $api.setStorage(NewFiData.wifiKey.wifiIp, wifiIp);
              $api.setStorage(NewFiData.wifiKey.apMac, apMac);
              if(phoneMac != '' || phoneMac != undefined){$api.setStorage(NewFiData.wifiKey.phoneMac, phoneMac);}

              lookForFWQ();
              console.log('.......收到原生的信息..........' + JSON.stringify(appData));
            }else{
              console.log(JSON.stringify( ret ));
            }
        });

        api.sendEvent({
            name: 'AppReceiveMessage',
            extra: {
                type: 1,
            }
        });

        //如监听事件
        api.addEventListener({
            name: 'indexCloseWin'
        }, function(ret, err) {
            api.closeWin({
            });
        });

        api.addEventListener({
        name: 'keyback'
        }, function(ret, err) {
          api.closeWin({
          });
        });
    }

    //测试数据
    function getInitDatas1() {
        $api.setStorage(NewFiData.wifiKey.networkType,1);
        $api.setStorage(NewFiData.wifiKey.wifiName, 'NewFi');
        $api.setStorage(NewFiData.wifiKey.wifiIp, '14.127.187.119');
        $api.setStorage(NewFiData.wifiKey.apMac, '70:3d:15:ec:26:51');
        $api.setStorage(NewFiData.wifiKey.phoneMac, '18:dc:56:12:12:1b');
        //如监听事件
        api.addEventListener({
            name: 'indexCloseWin'
        }, function(ret, err) {
            api.closeWin({
            });
        });
        lookForFWQ();
    }

    //寻找服务器
    function lookForFWQ() {
        var networkType = api.connectionType;
        console.log('........networkType.....'+networkType);
        if (networkType == '2g' || networkType == '3g' || networkType == '4g' || networkType == '4G'|| networkType == '3G'|| networkType == '2G') {
            mobileNetworkState();
        } else if (networkType == 'wifi') {
            lookForAgentFWQ();
        } else {
            document.getElementsByClassName('div_animation_prompt')[0].style.display = 'none';
            alert('请联上网络');
        }
    }
    //移动网络的网络状态
    function mobileNetworkState() {
        seekAgent.judgeCloudNetworkState(function(state) {
            common.getConsole('4G网络状态', state);
            document.getElementsByClassName('div_animation_prompt')[0].style.display = 'none';
            if (state) {
                common.getConsole('4G网络状态','..fwq..'+mCloudIP);
                $api.setStorage(NewFiData.appParameKey.networkstate,true);
                lookForAgentFWQSuccess(mCloudIP, 0, 0);
            } else {
                alert('连接不上互联网,请检查网络');
            }
        });
    }

    //测试代理，先用上次成功的代理，不行就去找代理
    function lookForAgentFWQ() {
        var fwq = $api.getStorage(NewFiData.wifiKey.agentFWQ);
        if (fwq == undefined) {
            fwq = '';
        }
        var parame = {
            userid: '0',
            visitType: 1,
            ip: $api.getStorage(NewFiData.wifiKey.wifiIp),
            mac: $api.getStorage(NewFiData.wifiKey.phoneMac),
            apMac: $api.getStorage(NewFiData.wifiKey.apMac),
            apName: $api.getStorage(NewFiData.wifiKey.wifiName)
        }
        var parame = common.getJSONParame(NewFiData.keyword.isAlive, JSON.stringify(parame));
        common.ajax(fwq, parame, 2, function(ret, err) {
            if (ret) {
                common.getConsole('测试代理结果', JSON.stringify(ret)+'..fwq..'+fwq);
                document.getElementsByClassName('div_animation_prompt')[0].style.display = 'none';
                if (ret.code == 1) {
                   if(ret.siteAreaid == 0){
                     lookForAgentFWQFail();
                   }else {
                     $api.setStorage(NewFiData.wifiKey.agentFWQ, fwq);
                     $api.setStorage(NewFiData.appParameKey.networkstate,false);
                     seekAgent.judgeCloudNetwork();
                     lookForAgentFWQSuccess(fwq, ret.siteAreaid, ret.netState);
                   }
                } else if (ret.code == 0) {
                    lookForAgentFWQFail();
                } else {
                    common.getShowViewError(21, ret.msg);
                }
            } else {
                lookForAgentFWQFail();
            }
        })
    }

    //找代理失败后的操作
    function lookForAgentFWQFail() {
        seekAgent.startSeekAgent(function(agentFwq, ret) {
            document.getElementsByClassName('div_animation_prompt')[0].style.display = 'none';
            common.getConsole('找代理结果', 'fwq' + agentFwq + JSON.stringify(ret));
            if (agentFwq) {
                if (ret.code == 1 || ret.code == 0) {
                    $api.setStorage(NewFiData.wifiKey.agentFWQ, agentFwq);
                    lookForAgentFWQSuccess(agentFwq, ret.siteAreaid, ret.netState);
                } else {
                    common.getShowViewError(21, ret.msg);
                }
            } else {
                common.getShowViewError(11, '没有找到代理服务器');
            }
        });
    }

    //找代理成功后的操作  netState 是否要执行断网 0 不需要 其他需要  api.winHeight
    function lookForAgentFWQSuccess(fwq, siteAreaid, netState) {
        $api.setStorage(NewFiData.appParameKey.appFWQ, fwq);
        $api.setStorage(NewFiData.appParameKey.siteAreaid, siteAreaid);
        var height = api.winHeight / 2;
        if (siteAreaid == 1) {
            var agentba = document.getElementsByClassName('div_index_agentba')[0];
            var agentba1 = document.getElementsByClassName('div_index_agent_1')[0];
            agentba.style.display = 'block';
            agentba.style.marginTop = height - 72 + 'px';
            agentba1.style.backgroundImage = "url(./image/sz_logo.png)";
        } else if (siteAreaid == 2) {
            var agentba = document.getElementsByClassName('div_index_agentba')[0];
            var agentba1 = document.getElementsByClassName('div_index_agent_1')[0];
            agentba.style.display = 'block';
            agentba.style.marginTop = height - 72 + 'px';
            agentba1.style.backgroundImage = "url(./image/szy_logo.png)";
        } else {
            var agentba = document.getElementsByClassName('div_index_newfiba')[0];
            agentba.style.display = 'block';
            agentba.style.marginTop = height - 97 + 'px';
        }

        var view = document.getElementsByClassName('div_animation_prompt')[0];
        seekAgent.getSaveCombo(0,null);
        login.automaticLogin(view);
    }
</script>

</html>
